'''
将电量低于临界值的车辆派去最近的充电区

对外接口：
- arrange_aiv(aivs, destinations, distance_matrix) -> list
'''

from common import InsDeparture


def arrange_aiv(aiv, signal, destinations, distance_matrix) -> InsDeparture:
    '''
    - aiv 请求被调度的小车信息
    - destinations 符合要求的所有目的地
    - distance_matrix 距离矩阵，获取起点到终点的距离
    '''
    zone, dis = find_nearest_zone(aiv['position'], destinations, distance_matrix)

    return InsDeparture(aiv, zone, signal)


def find_nearest_zone(start, zones, distance_matrix):
    '''
    找到离起始位置最近的充电区，将之与无人车匹配
    '''
    min_distance = distance_matrix.distance_get(start, zones[0])
    nearest_zone = zones[0]

    for zone in zones[1:]:
        if distance_matrix.distance_get(start, zone) < min_distance:
            min_distance = distance_matrix.distance_get(start, zone)

    return nearest_zone, min_distance